package top.twoteam.equipment.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.twoteam.equipment.entity.EquipmentInfo;
import top.twoteam.equipment.entity.EquipmentInfoExample;
import top.twoteam.equipment.mapper.EquipmentInfoMapper;
import top.twoteam.equipment.utils.PageRequest;
import top.twoteam.equipment.utils.WEBUtil;

import java.util.List;

/**
 * @author binghan
 * @version 1.0
 * @description: TODO 设备业务
 * @date 2022/6/2 17:17
 */
@Service
public class EquipmentService {

    @Autowired
    EquipmentInfoMapper eIM;

    /**
     * 查询所有设备信息
     * @return
     */
    public List<EquipmentInfo> queryAllEquipmentInfos() {
        // 创建查询条件
        EquipmentInfoExample eIE  = new EquipmentInfoExample();
        EquipmentInfoExample.Criteria criteria = eIE.createCriteria();
        return eIM.selectByExample(eIE);
    }

    /**
     *  条件+分页 查询设备信息
     * @param req
     * @return
     */
    public PageInfo<EquipmentInfo> queryByCondition(PageRequest<EquipmentInfo> req) {


        // 设定分页 Num 和 Size
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        // 创建查询条件
        EquipmentInfoExample eIE  = new EquipmentInfoExample();
        EquipmentInfoExample.Criteria criteria = eIE.createCriteria();
        EquipmentInfo equipmentInfo = req.getData();


        if(!WEBUtil.isEmpty(equipmentInfo)) {

            // id 等值查询
            if(!WEBUtil.isEmpty(equipmentInfo.getEquipmentid())) {
                criteria.andEquipmentidEqualTo(equipmentInfo.getEquipmentid());
            }
            // 设备名 模糊查询
            if(!WEBUtil.isEmpty(equipmentInfo.getEquipmentname())) {
                criteria.andEquipmentnameLike("%"+equipmentInfo.getEquipmentname()+"%");
            }
            //  todo 设备类型 等值查询
            if(!WEBUtil.isEmpty(equipmentInfo.getEquipmenttype())) {
                criteria.andEquipmenttypeEqualTo(equipmentInfo.getEquipmenttype());
            }
            //报警类型 等值查询
            if(!WEBUtil.isEmpty(equipmentInfo.getAlarmtype())) {
                    criteria.andAlarmtypeEqualTo(equipmentInfo.getAlarmtype());
            }
            //街道位置 模糊查询
            if(!WEBUtil.isEmpty(equipmentInfo.getAddress())) {
                criteria.andAddressLike("%"+equipmentInfo.getAddress()+"%");
            }
            // 连接方式 具体查询
            if(!WEBUtil.isEmpty(equipmentInfo.getLinktype())) {
                criteria.andLinktypeEqualTo(equipmentInfo.getLinktype());
            }
            // 维护port 具体查询
            if(!WEBUtil.isEmpty(equipmentInfo.getMaintainport())) {
                criteria.andMaintainportEqualTo(equipmentInfo.getMaintainport());
            }
            // 上次维护uid 具体查询
            if(!WEBUtil.isEmpty(equipmentInfo.getLastmaintainuid())) {
                criteria.andLastmaintainuidEqualTo(equipmentInfo.getLastmaintainuid());
            }
            // 上次维护时间 具体查询
            if(!WEBUtil.isEmpty(equipmentInfo.getLastmaintaintime())) {
                criteria.andLastmaintaintimeEqualTo(equipmentInfo.getLastmaintaintime());
            }

        }

        // 返回查询的PageInfo
        List<EquipmentInfo> equipmentInfos = eIM.selectByExample(eIE);
        PageInfo<EquipmentInfo> equipmentInfoPageInfo = new PageInfo<>(equipmentInfos);
        return equipmentInfoPageInfo;
    }

    /**
     * 批量删除设备
     * @param idList id 删除
     * @return
     */
    public boolean deleteEquimentInfo(List<Integer> idList){
        if(!WEBUtil.isEmpty(idList)) {
            // 创建查询条件
            EquipmentInfoExample eIE  = new EquipmentInfoExample();
            EquipmentInfoExample.Criteria criteria = eIE.createCriteria();
            criteria.andEquipmentidIn(idList);
            // 批量删除
            int i = eIM.deleteByExample(eIE);
            if( i!= 0) {

            return true;
            } else {
                return false;
            }
        }
        return false;
    }

    //更新数据
    public boolean updateEquipmentInfo(EquipmentInfo equipmentInfo){
        if(!WEBUtil.isEmpty(equipmentInfo)) {
            if(!WEBUtil.isEmpty(equipmentInfo.getEquipmentid())) {
                this.eIM.updateByPrimaryKeySelective(equipmentInfo);

            }
        }
        return  true;
    }

    //增加数据
    public boolean insertEquipmentInfo(EquipmentInfo equipmentInfo){
        if(!WEBUtil.isEmpty(equipmentInfo)) {
                this.eIM.insertSelective(equipmentInfo);
        }
        return  true;
    }

}